package com.android.frame.ui;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.view.View;


import com.android.frame.R;
import com.android.frame.view.dialog.FormBotomDialogBuilder;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoImpl;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.InvokeParam;
import com.jph.takephoto.model.TContextWrap;
import com.jph.takephoto.permission.InvokeListener;
import com.jph.takephoto.permission.PermissionManager;
import com.jph.takephoto.permission.TakePhotoInvocationHandler;

import java.io.File;

/**
 * Created by pmj on 2017/5/11.
 */

public abstract class BasePhotoActivity extends BaseActivity implements TakePhoto.TakeResultListener, InvokeListener {

    private FormBotomDialogBuilder photoDialog;
    private InvokeParam invokeParam;
    private TakePhoto takePhoto;

    protected  boolean isChoosePhoto;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getTakePhoto().onActivityResult(requestCode, resultCode, data);
    }


    public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
        PermissionManager.TPermissionType type = PermissionManager.checkPermission(TContextWrap.of(this), invokeParam.getMethod());
        if (PermissionManager.TPermissionType.WAIT.equals(type)) {
            this.invokeParam = invokeParam;
        }
        return type;
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.TPermissionType type = PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.handlePermissionsResult(this, type, invokeParam, this);
    }

    public Uri getImageUri() {
        File file = new File("/sdcard/" + getResources().getString(R.string.app_name) + "/img/", +System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        return Uri.fromFile(file);
    }


    public void initPhotoDialog() {
        if (photoDialog != null) {
            photoDialog.show();
            return;
        }

        photoDialog = new FormBotomDialogBuilder(this);
        View view = getLayoutInflater().inflate(R.layout.choose_photo_layout, null);
        photoDialog.setFB_AddCustomView(view);


        View.OnClickListener listenrer = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (v.getId() == R.id.fbtn_one) {
                    CropOptions cropOptions1 = new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(false).create();
//                                .setOutputX((int) getResources().getDimension(R.dimen.x60)).setOutputY((int) getResources().getDimension(R.dimen.x60)).create();
                    CompressConfig config1 = new CompressConfig.Builder().setMaxSize(40 * 1024).create();
                    getTakePhoto().onEnableCompress(config1, true);
//                        getTakePhoto().onPickMultiple(1);
                    getTakePhoto().onPickFromCapture(getImageUri());
                    isChoosePhoto = true;
                    photoDialog.dismiss();
                }

                if (v.getId() == R.id.fbtn_two) {
                    CropOptions cropOptions = new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(false).create();
//                                .setOutputX((int) getResources().getDimension(R.dimen.x60)).setOutputY((int) getResources().getDimension(R.dimen.x60)).create();
                    CompressConfig config = new CompressConfig.Builder().setMaxSize(40 * 1024).create();
                    getTakePhoto().onEnableCompress(config, true);
                    getTakePhoto().onPickMultiple(1);
//                        getTakePhoto().onPickFromDocuments();
                    photoDialog.dismiss();
                    isChoosePhoto = true;
                }

                if (v.getId() == R.id.fbtn_three) {
                    photoDialog.dismiss();
                }
            }
        };

        view.findViewById(R.id.fbtn_one).setOnClickListener(listenrer);
        view.findViewById(R.id.fbtn_two).setOnClickListener(listenrer);
        view.findViewById(R.id.fbtn_three).setOnClickListener(listenrer);
        photoDialog.show();
    }


    /**
     * 获取TakePhoto实例
     *
     * @return
     */
    public TakePhoto getTakePhoto() {
        if (takePhoto == null) {
            takePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this, this));
        }
        return takePhoto;
    }

}
